package com.example.template.common.storage.impl;

import com.example.template.common.api.ApiCodeConstant;
import com.example.template.common.exception.GlobalException;
import com.example.template.common.storage.FileOperation;
import com.example.template.utils.FileKit;
import com.obs.services.ObsClient;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.io.ByteArrayInputStream;


@Component
public class HwyunObsStorageImpl extends AbstractStorage implements FileOperation {
    private static Logger logger = LoggerFactory.getLogger(HwyunObsStorageImpl.class);
    private static final String obs_endpoint = "hwyun_obs_endpoint";
    private static final String obs_bucket   = "hwyun_obs_bucket";
    private static final String obs_key      = "hwyun_obs_key";
    private static final String obs_secret   = "hwyun_obs_secret";
    private static final String obs_src      = "hwyun_obs_src";

    @Override
    public String writeToStore(byte[] bytes, String pathAndFileName) throws Exception {
        String endpoint = options.getValue(obs_endpoint);
        String bucket = options.getValue(obs_bucket);
        String src = options.getValue(obs_src);

        if (StringUtils.isAnyBlank(endpoint, bucket)) {
            throw new GlobalException(ApiCodeConstant.RESULT_OTHER_EXCEPTION,"请先在后台设置华为云配置信息");
        }

        if (StringUtils.isBlank(src)) {
            src = "";
        } else {
            if (src.startsWith("/")) {
                src = src.substring(1);
            }

            if (!src.endsWith("/")) {
                src = src + "/";
            }
        }

        String key = DigestUtils.md5DigestAsHex(bytes);
        String path = src + key + FileKit.getSuffix(pathAndFileName);
        ObsClient client = builder();
        client.putObject(bucket, path, new ByteArrayInputStream(bytes));
        return "//" + bucket.trim() + "." + endpoint.trim() + "/" + path;
    }

    @Override
    public void deleteFile(String storePath) {
        String bucket = options.getValue(obs_bucket);
        String endpoint = options.getValue(obs_endpoint);
        String path = StringUtils.remove(storePath, "//" + bucket.trim() + "." + endpoint.trim() + "/");
        ObsClient client = builder();
        try {
            client.doesObjectExist(bucket, path);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private ObsClient builder() {
        String endpoint = options.getValue(obs_endpoint);
        String accessKeyId = options.getValue(obs_key);
        String accessKeySecret = options.getValue(obs_secret);

        if (StringUtils.isAnyBlank(endpoint, accessKeyId, accessKeySecret)) {
            throw new GlobalException(ApiCodeConstant.RESULT_OTHER_EXCEPTION,"请先在后台设置华为云配置信息");
        }
        return new ObsClient(accessKeyId, accessKeySecret,endpoint);
    }
}
